//
//  GeneratorViewController.swift
//  01-界面搭建
//
//  Created by 饶航 on 16/6/27.
//  Copyright © 2016年 Via. All rights reserved.
//

import UIKit

class GeneratorViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var TextFile: UITextField!
   

}

extension GeneratorViewController {
    @IBAction func generateQRCode() {
//        0.退出键盘
        view.endEditing(true)
        
//        1.获取用户输入的内容
        guard let inputMsg = TextFile.text else{
        print("没有输入内容")
            return
        }
        guard inputMsg.characters.count != 0 else {
            print("没有输入具体内容")
            return
        }
        
//        2.讲内容生成二维码
//        2.1创建过滤器(滤镜) :CIQRCodeGenerator -->创建二维码的过滤器
        let filter = CIFilter(name: "CIQRCodeGenerator")!
        
//        2.2给滤镜设置内容(KVC)
        let inputData = inputMsg.dataUsingEncoding(NSUTF8StringEncoding)
        filter.setValue(inputData, forKey: "inputMessage")
        
//        设置二维码的容错率
        filter.setValue("H", forKey: "inputCorrectionLevel")
        
//        2.3获取生成的二维码图片
       guard let outputImage = filter.outputImage else{
            return
        }
        
//        3.生成高清的图片
        let hdImage = createHDImage(outputImage)
        
        
//        讲生成的二维码显示在imageview
        imageView.image = hdImage

        }
    
   private func createHDImage(ciImage: CIImage) -> UIImage {
        //        1.放大图片
        let transform = CGAffineTransformMakeScale(10, 10)
        let newCIImage = ciImage.imageByApplyingTransform(transform)
        //        2.创建UIimage对象
        return UIImage(CIImage: newCIImage)
    }

}










